package com.acmen.easyapi.validation.validator;

import com.acmen.easyapi.constant.ValidateExp;
import com.acmen.easyapi.util.IdNoUtil;
import com.acmen.easyapi.util.StringUtil;
import com.acmen.easyapi.validation.DataTypeValidator;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 身份证号校验器
 *
 * @author acmen
 * @date 2022-04-06
 */
@Component
public class IDCardValidator implements DataTypeValidator {

    public boolean matches(String exp) {
        return ValidateExp.VALIDATE_EXP_IDNO.equals(exp);
    }

    @Override
    public void validate(String exp, Object data, List<String> messages) {
        Object res = IdNoUtil.validate(data.toString());
        if (res instanceof String) {
            messages.add(data+""+ res);
        }
    }

    /**
     * 校验说明
     *
     * @return
     */
    @Override
    public String getNote(String exp) {
        if(StringUtil.isNotEmpty(exp)){
            return  "数据必需是合法的身份证号；";
        }
        return ValidateExp.VALIDATE_EXP_IDNO + "：数据必需是合法的身份证号；";
    }
}
